<template>
  <div class="container">
    <div class="app-container">
      <div>
        <el-button type="primary" size="mini" @click="iShow">添加角色</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="name"
          label="角色"
          width="300"
          align="center"
        >
          <template #default="{row}">
            <span v-if="!row.flag">{{ row.name }}</span>
            <el-input v-else v-model="row.editRole.name" :placeholder="row.name" />
          </template>
        </el-table-column>
        <el-table-column
          prop="state"
          label="状态"
          width="300"
          align="center"
        >
          <template v-slot="{ row }">
            <el-tag v-if="!row.flag" disable-transitions :type="row.state ? 'success' : 'danger'">  {{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }} </el-tag>
            <el-switch
              v-else
              v-model="row.editRole.state"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述"
          width="500"
          align="center"
        >
          <template #default="{row}">
            <span v-if="!row.flag">{{ row.editRole.description }}</span>
            <el-input v-else v-model="row.editRole.description" :placeholder="row.description" />
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          align="center"
        >
          <template #default="{row}">
            <template v-if="!row.flag">
              <el-button type="text" size="mini" @click="openPower(row.id)">分配权限</el-button>
              <el-button type="text" size="mini" @click="openEdit(row)">编辑</el-button>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                style="margin-left:10px"
                @onConfirm="delRole(row.id)"
              >
                <el-button slot="reference" type="text" size="mini">删除</el-button>
              </el-popconfirm>
            </template>
            <template v-else>
              <el-button type="primary" size="mini" style="border-color:none" @click="updateRole(row)">确认</el-button>
              <el-button size="mini" @click="removeSlot(row)">取消</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>

      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          :page-sizes="[2, 4, 5, 10]"
          :page-size="pageinfo.pageSize"
          :current-page="pageinfo.currentPage"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
      <RoleDialog :dialog-visible.sync="dialogVisible" @update-list="render" />
      <PowerDialogVue :id="id" :dialog-power-visible.sync="dialogPowerVisible" />
    </div>
  </div>
</template>
<script>
import RoleDialog from '@/views/role/components/role-dialog.vue'
import { delSysRole, getSysRole, putSysRole } from '@/api/role/role'
import PowerDialogVue from './components/power-dialog.vue'
export default {
  name: 'Role',
  components: { RoleDialog, PowerDialogVue },
  data() {
    return {
      tableData: [],
      pageinfo: {
        currentPage: 1,
        pageSize: 10
      },
      total: 50,
      dialogVisible: false,
      dialogPowerVisible: false,
      flag: true,
      id: null
    }
  },
  created() {
    this.render()
  },
  methods: {
    handleSizeChange(val) {
      this.pageinfo.pageSize = val
      this.render()
    },
    async render() {
      const res = await getSysRole({
        page: this.pageinfo.currentPage,
        pageSize: this.pageinfo.pageSize
      })
      this.tableData = res.rows
      res.rows.forEach(row => {
        this.$set(row, 'editRole', { ...row })
      })
    },
    handleCurrentChange(val) {
      this.pageinfo.currentPage = val
      this.render()
    },
    iShow() {
      this.dialogVisible = true
    },
    // 打开分配权限弹窗
    openPower(id) {
      this.dialogPowerVisible = true
      this.id = id
    },
    // 删除角色
    async delRole(id) {
      try {
        if (this.tableData.length === 1 && this.pageinfo.page !== 1) {
          this.pageinfo.page--
        }
        await delSysRole(id)
        await this.render()
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 打开编辑
    openEdit(row) {
      this.$set(row, 'flag', true)
    },
    // 取消编辑
    removeSlot(row) {
      this.$set(row, 'flag', false)
      this.render()
    },
    // 修改角色
    async updateRole(row) {
      try {
        await putSysRole(row.editRole.id, row.editRole)
        this.removeSlot(row)
        this.$message.success('编辑成功')
      } catch (error) {
        console.log(error)
        this.$message.error('编辑失败')
      }
    }
  }

}
</script>
<style lang="scss" scoped>
.el-table {
  margin-top: 10px;
  border-top: 0;
  border-right: 0;
  border-left: 0;
}
::v-deep .el-table__row td{
   border-right:0;
}
::v-deep .has-gutter th {
   border-right:0;
}

</style>
